package com.tadpole.music.view.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.TabAdapter;
import com.tadpole.music.bean.question.QuestionRankBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.QuestionRankIView;
import com.tadpole.music.presenter.question.QuestionRankPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.question.SimulationPracticeActivity;
import com.tadpole.music.view.fragment.question.SimulationFragment;
import com.tadpole.music.view.fragment.question.SimulationHomeFragment;
import com.tadpole.music.view.fragment.question.SimulationPracticeFragment;
import com.tadpole.music.view.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 在线套题
 * Created By 孙岩
 * on 2020/9/27
 */
public class OnlineExercisesActivity extends BaseActivity implements QuestionRankIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //name
    private TextView tvName;
    //TabLayout
    private TabLayout tabLayout;
    //viewPager
    private NoScrollViewPager viewPager;
    private TabAdapter tabAdapter;
    //tab集合
    private List<String> tabList = new ArrayList<>();
    //viewpager绑定的fragment的集合
    private List<Fragment> fragmentList = new ArrayList<>();
    //获取等级
    private QuestionRankPresenter questionRankPresenter;
    //学院id
    private int college_id;
    //学院名称
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simulation_practice);
        name = getIntent().getStringExtra("name");
        college_id = getIntent().getIntExtra("college_id",0);
        initViews();
        initListeners();
        questionRankPresenter = new QuestionRankPresenter();
        questionRankPresenter.attachView(this);
        questionRankPresenter.getQuestionRank(college_id);
    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("在线套题");
        tvName = findViewById(R.id.tvName);
        tvName.setText(name);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
               OnlineExercisesActivity.this.finish();
            }
        });
    }

    @Override
    public void showQuestionRank(List<QuestionRankBean.DataBean> list) {
        for(int i=0;i<list.size();i++){
            tabList.add(list.get(i).getName());
            fragmentList.add(SimulationHomeFragment.newInstance(String.valueOf(college_id), String.valueOf(list.get(i).getId())));
        }

        tabAdapter = new TabAdapter(fragmentList, tabList, getSupportFragmentManager(), this);
        viewPager.setAdapter(tabAdapter);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(tabAdapter.getTabView(i));
            }

            if (i == 0) {
                //设置默认进来第一个是选中状态
                TabLayout.Tab a = tabLayout.getTabAt(0);
                View view1 = a.getCustomView();
                if (null != view1 && view1 instanceof TextView) {
                    ((TextView) view1).setTextSize(16);
                    ((TextView) view1).setTextColor(getResources().getColor(R.color.color_33));
                }
            } else {
                //设置默认进来第二个是不选中状态
                TabLayout.Tab b = tabLayout.getTabAt(1);
                View view2 = b.getCustomView();
                if (null != view2 && view2 instanceof TextView) {
                    ((TextView) view2).setTextSize(13);
                    ((TextView) view2).setTextColor(getResources().getColor(R.color.color_65));
                }
            }
        }


        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //切换导航菜单时候的监听
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), true);
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextSize(16);
                    ((TextView) view).setTextColor(getResources().getColor(R.color.color_33));
                    ((TextView) view).setTextAppearance(OnlineExercisesActivity.this, R.style.TabLayoutTextStyle);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextSize(13);
                    ((TextView) view).setTextColor(getResources().getColor(R.color.color_65));
                }
            }


            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void show401() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        SpUtil.getInstance(this).putString(Constant.TOKEN,"");
    }
}
